package com.hx.mylombok.task.impl;

import com.hx.mylombok.task.AbstractFileTask;
import com.hx.mylombok.task.FileTask;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
/**
 * @author guoxiaolin
 * @description
 * @since 2019/3/27 15:10
 */
public class ProxyControllerTask extends AbstractFileTask implements FileTask {

    public ProxyControllerTask(String className) {
        super(className);
    }

    @Override
    public void generateFile(Map<String, String> setterMap, ProcessingEnvironment processingEnv) throws IOException {

        JavaFileObject builderProxyControllerFile = processingEnv.getFiler().
                createSourceFile(packageName + ".controller.proxy.ProxyController"+sufixName);
        
        try (PrintWriter out = new PrintWriter(builderProxyControllerFile.openWriter())) {

            if (packageName != null) {
                out.print("package ");
                out.print(packageName);
                out.println(".controller.proxy;");
                out.println();
            }
            out.println("import com.hx.battery.common.hibernate.controller.proxy.AbstractProxyController;");
            out.println("import com.hx.battery.common.hibernate.service.IService;");
            out.println("import "+packageName+".domain."+sufixName+";");
            out.println("import "+packageName+".service."+simpleClassName+";");
            out.println("import io.swagger.annotations.Api;");
            out.println("import org.springframework.beans.factory.annotation.Autowired;");
            out.println("import org.springframework.web.bind.annotation.RequestMapping;");
            out.println("import org.springframework.web.bind.annotation.RestController;");
            out.println();
            out.println("@Api(description = \"xxxx\")");
            out.println("@RestController");
            out.println("@RequestMapping(\"/proxy/"+sufixName+"\")");
            out.println("public class ProxyController"+sufixName+" extends AbstractProxyController<"+sufixName+"> {");
            out.println();
            out.println("    private "+simpleClassName+" service"+sufixName+";");
            out.println("    public ProxyController"+sufixName+"(){}");
            out.println("    @Autowired");
            out.println("    public ProxyController"+sufixName+"( "+simpleClassName+" service"+sufixName+"){");
            out.println("        this.service"+sufixName+" = service"+sufixName+";");
            out.println("    }");
            out.println("    @Override");
            out.println("    public IService<"+sufixName+"> getServcie() {");
            out.println("        return service"+sufixName+";");
            out.println("    }");
            out.println();
            if (setterMap.size() == 0){
                setterMap.forEach((methodName, argumentType) -> {
                    String args[] = argumentType.split(",");
                    String returnType = args[0];
                    int sumargs = args.length;
                    out.println("    @PostMapping(\"/" + methodName + "\")");
                    out.print("    public " + returnType + " " + methodName + " ");
                    out.print("(");
                    String argline = "";
                    String primitive= "int,long,short,char,float,double,boolean,byte";
                    for (int i = 1; i < sumargs; i++){
                        if (primitive.indexOf(args[i]) != -1){//包含
                            out.print(" @RequestParam(value = \"a" + i + "\" ) ");
                        }else{
                            out.print(" @RequestBody ");
                        }
                        if (i<sumargs-1){
                            argline = argline + "a" + i + ", ";
                            out.print(args[i] + " a" + i + ",");
                        }else{
                            argline = argline + "a" + i;
                            out.print(args[i] + " a" + i);
                        }
                    }
                    out.println(") {");
                    out.println("        return serviceObj." + methodName + "(" + argline + ");");
                    out.println("    }");
                });
            }

            out.println("}");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            logger.info("ProxyControllerTask of " + simpleClassName + " is over");
        }
    }
}
